<?php
	require_once(realpath(dirname(__FILE__)) . "/Baseobj.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Contenudesc.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Image.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Document.class.php");
	require_once(realpath(dirname(__FILE__)) . "/Reecriture.class.php");

	class Contenu extends Baseobj{

		var $id;
		var $datemodif;
		var $ligne;
		var $dossier;
		var $classement;

		const TABLE="contenu";
		var $table=self::TABLE;

		var $bddvars=array("id", "datemodif", "ligne", "dossier", "classement");

		function __construct($id = 0){
			parent::__construct();

			if($id > 0)
 			  $this->charger($id);
		}


		function charger($id){
			return $this->getVars("select * from $this->table where id=\"$id\"");
		}

		function changer_classement($id, $type){

			$this->charger($id);
			$remplace = new Contenu();

			if($type == "M")
				$res = $remplace->getVars("select * from $this->table where dossier=\"" . $this->dossier. "\" and classement<" . $this->classement . " order by classement desc limit 0,1");

			else if($type == "D")
				$res  = $remplace->getVars("select * from $this->table where dossier=\"" . $this->dossier. "\" and classement>" . $this->classement . " order by classement limit 0,1");

			if(! $res)
				return "";

			$sauv = $remplace->classement;

			$remplace->classement = $this->classement;
			$this->classement = $sauv;

            $remplace->maj();
            $this->maj();

		}

		function delete($requete){
				$resul = mysql_query($requete, $this->link);
				CacheBase::getCache()->reset_cache();
		}

		function supprimer(){

			if ($this->id == 0 || $this->id == "") return;

			$image = new Image();
			
			$query = "select * from $image->table where contenu=\"" . $this->id . "\" AND dossier=\"0\" AND produit=\"0\" AND rubrique=\"0\" ";
			$resul = mysql_query($query, $image->link);
			while($row = mysql_fetch_object($resul)){
				$tmp = new Image();
				$tmp->charger($row->id);
				$tmp->supprimer();

			}

			$document = new Document();

			$query = "select * from $document->table where contenu=\"" . $this->id . "\" AND dossier=\"0\" AND produit=\"0\" AND rubrique=\"0\" ";
			$resul = mysql_query($query, $document->link);
			while($row = mysql_fetch_object($resul)){
				$tmp = new Document();
				$tmp->charger($row->id);
				$tmp->supprimer();

			}

			$contenudesc =  new Contenudesc();


			$this->delete("delete from $this->table where id=\"$this->id\"");
			$this->delete("delete from $contenudesc->table where contenu=\"$this->id\"");

			$queryclass="select * from $this->table where dossier=$this->dossier order by classement";
			$resclass = mysql_query($queryclass);

			if(mysql_num_rows($resclass) > 0){

				$i=1;
				while($rowclass = mysql_fetch_object($resclass)){
					$cont = new Contenu();
					$cont->charger($rowclass->id);
					$cont->classement = $i;
					$cont->maj();
					$i++;
				}
			}


			return 1;

		}


	}

?>